package edu.gatech.cs2340.mule.view;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeListener;

import edu.gatech.cs2340.mule.presenter.MuleGamePresenter;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;

/**
 * Represents the panel the user will use to purchase items from the store
 *
 * @author J.R. O'Donnell
 *
 */
public class BuyPanel extends JPanel {

	private static final int HGAP = 50;
	private static final int VGAP = 50;
	
	private JButton buyButton;
	private JButton cancelButton;
	private JComboBox resourceSelectionComboBox;
	private JSpinner resourceAmountSpinner;
	private JLabel priceLabel;
	
	/**
	 * Initializes the panel for the user to buy resources
	 * 
	 */
	public BuyPanel() {
		GridLayout gl = new GridLayout(3, 2);
		gl.setHgap(HGAP);
		gl.setVgap(VGAP);
		
		setLayout(gl);
		setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		
		resourceSelectionComboBox = new JComboBox();
		resourceSelectionComboBox.setModel(new DefaultComboBoxModel(new String[]
				{"Food","Energy","Smithore"}));
		resourceSelectionComboBox.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		resourceSelectionComboBox.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		add(resourceSelectionComboBox);
		
		resourceAmountSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 16, 1));
		resourceAmountSpinner.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		resourceAmountSpinner.setBackground(MuleGamePresenter.BACKGROUND_COLOR);		
		
		for(Component c : resourceAmountSpinner.getComponents()) {
			c.setBackground(MuleGamePresenter.FOREGROUND_COLOR);
			c.setForeground(MuleGamePresenter.BACKGROUND_COLOR);
		}
		
		add(resourceAmountSpinner);
	
		buyButton = new JButton();
		buyButton.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		buyButton.setBorder(new LineBorder(MuleGamePresenter.BACKGROUND_COLOR, 2));
		buyButton.setIcon(new ImageIcon("Images\\icon_buy.png"));
		add(buyButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setBackground(MuleGamePresenter.FOREGROUND_COLOR);
		cancelButton.setForeground(MuleGamePresenter.BACKGROUND_COLOR);
		add(cancelButton);
		
		priceLabel = new JLabel("<html><font size=+2>Price: 0");
		priceLabel.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(priceLabel);
	}
	
	/**
	 * Adds the action listener to the buy button
	 * 
	 * @param a The action listener for the buy button
	 */
	public void addBuyListener(ActionListener a) {
		buyButton.addActionListener(a);
	}
	
	/**
	 * Adds the action listener to the cancel button
	 * 
	 * @param a The action listener for the cancel button
	 */
	public void addCancelListener(ActionListener a) {
		cancelButton.addActionListener(a);
	}
	
	/**
	 * Adds the change listener to the resource amount spinner
	 * 
	 * @param c The change listener for the spinner
	 */
	public void addSpinnerListener(ChangeListener c) {
		resourceAmountSpinner.addChangeListener(c);
	}
	
	/**
	 * Adds the item listener to the resource selection combo box
	 * 
	 * @param i The change listener for the spinner
	 */
	public void addComboListener(ItemListener i) {
		resourceSelectionComboBox.addItemListener(i);
	}
	
	/**
	 * Changes the color of the price label to red for when something cannot be purchased
	 * 
	 */
	public void recolorPriceLabel() {
		priceLabel.setForeground(new Color(153, 0, 0));
	}
	
	/**
	 * 
	 * @param price The price to display to the user
	 */
	public void updateBuyPanelPriceLabel(int price) {
		priceLabel.setText("<html><font size=+2>Price: " + price);
		priceLabel.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
	}
	
	/**
	 * 
	 * @param amount The maximum amount of resources the user can purchase
	 */
	public void updateBuyPanelSpinnerMax(int amount) {
		resourceAmountSpinner.setModel(new SpinnerNumberModel(0, 0, amount, 1));
	}
	
	/**
	 * 
	 * @return The resource the user selected to purchase
	 */
	public String getSelectedResource() {
		return (String) resourceSelectionComboBox.getSelectedItem();
	}
	
	/**
	 * 
	 * @return The amount of resource the user decided to purchase
	 */
	public int getSelectedAmount() {
		return (int) resourceAmountSpinner.getValue();
	}
}
